using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;

namespace Devsoft.Utilities
{
    /// <summary>
    /// This class is used to store general options for your application
    /// The default configuration file is config.xml
    /// </summary>
    public class Option
    {
        #region Property FileName
        protected static string _FileName = "config.xml";
        /// <summary>
        /// Default filename of the configuration file.
        /// </summary>
        public static string FileName
        {
            get
            {
                return _FileName;
            }
            set
            {
                _FileName = value;
            }
        }
        #endregion


        #region Property Instance
        protected static Option _Instance = null;
        /// <summary>
        /// The currently instance of Option class
        /// </summary>
        public static Option Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new Option();
                }//End If
                return _Instance;
            }
        }
        #endregion



        /// <summary>
        /// Open configuration file for reading and writing
        /// </summary>        
        protected Option()
        {
            //
            // TODO: Add constructor logic here
            //
            _xDoc = new XmlDocument();
            _xDoc.Load(_FileName);
        }


        /// <summary>
        /// Set value for an option
        /// </summary>
        /// <param name="sName">Option's name</param>
        /// <param name="sValue">Option's value</param>
        public void SetOptionValue(string sName, object sValue)
        {
            XmlNode node = _xDoc.SelectSingleNode(STR_CONFIG_NODE + sName);
            if (node == null) //not exists, create new one
            {
                node = _xDoc.CreateElement(sName);
                _xDoc.SelectSingleNode(STR_CONFIG_NODE).AppendChild(node);
            }
            node.InnerText = sValue.ToString();
        }

        /// <summary>
        /// Get value of an option
        /// </summary>
        /// <param name="sName">Option's name</param>
        /// <returns>value of the option</returns>
        public string GetOptionValue(string sName)
        {
            XmlNode node = _xDoc.SelectSingleNode(STR_CONFIG_NODE + sName);
            return node.InnerText;
        }

        /// <summary>
        /// Get value of an option as an Integer
        /// </summary>
        /// <param name="sName">Option's name</param>
        /// <returns>value of the option</returns>
        public int GetOptionValueI(string sName)
        {
            return System.Convert.ToInt32(GetOptionValue(sName));
        }

        /// <summary>
        /// Get value of an option as a double
        /// </summary>
        /// <param name="sName">Option's name</param>
        /// <returns>value of the option</returns>
        public double GetOptionValueD(string sName)
        {
            return System.Convert.ToDouble(GetOptionValue(sName));
        }




        #region Save/Load
        /// <summary>
        /// Save the currently options to the file named _Filename
        /// </summary>
        public void Save()
        {               
            _xDoc.Save(_FileName);
        }
        /// <summary>
        /// Save the currently options to the file named  sFileName
        /// </summary>
        /// <param name="sFileName">The filename of the new file</param>
        public void Save(string sFileName)
        {
            _FileName = sFileName;
            Save();
        }
        #endregion

        /// <summary>
        /// Holds the currently configuration file as a XmlDocument
        /// </summary>
        protected XmlDocument _xDoc = null;
        protected const string STR_CONFIG_NODE = "/config/";


    }
}
